package gov.cdc.epiinfo;

import java.util.Hashtable;
import java.util.LinkedList;

import android.app.Activity;

public class AppManager {

	private static LinkedList<Activity> activities;
	private static EpiDbHelper currentDatabase; 
	private static Hashtable<String, FormMetadata> forms;
	private static Hashtable<Activity, String> guids;
	
	public static void AddFormMetadata(String name, FormMetadata formMetadata)
	{
		if (forms == null)
		{
			forms = new Hashtable<String, FormMetadata>();
		}
		forms.put(name, formMetadata);
	}
	
	public static FormMetadata GetFormMetadata(String name)
	{
		return forms.get(name);
	}
	
	public static void AddFormGuid(Activity activity, String guid)
	{
		if (guids == null)
		{
			guids = new Hashtable<Activity, String>();
		}
		guids.put(activity, guid);
	}
	
	public static String GetFormGuid(Activity activity)
	{
		return guids.get(activity);
	}
	
	public static void SetCurrentDatabase(EpiDbHelper db)
	{
		currentDatabase = db;
	}
	
	public static EpiDbHelper GetCurrentDatabase()
	{
		return currentDatabase;
	}
	
	public static void Started(Activity activity)
	{
		if (activities == null)
		{
			activities = new LinkedList<Activity>();
		}
		activities.add(activity);
		if (activities.size() == 1)
		{
			GeoLocation.BeginListening(activity);
		}
	}
	
	public static void Closed(Activity activity)
	{
		if (activities.contains(activity))
		{
			activities.remove(activity);
		}
		if (activities.size() == 0)
		{
			GeoLocation.StopListening();
		}
	}
	
}
